package com.ruoyi.fetv.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.aspect.annotation.AutoDict;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.fetv.controller.vo.Label;
import com.ruoyi.fetv.controller.vo.navigation.AddReq;
import com.ruoyi.fetv.controller.vo.navigation.EditReq;
import com.ruoyi.fetv.controller.vo.navigation.RespVO;
import com.ruoyi.fetv.domain.FetvNavigationContext;
import com.ruoyi.fetv.service.IFetvNavigationContextService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 栏目Controller
 *
 * @author Jack
 * @date 2023-03-07
 */
@Api(value = "/fetv/context", tags = {"栏目管理"})
@RestController
@RequestMapping("/fetv/context")
@Validated
@AutoDict
public class FetvNavigationContextController extends BaseController {
    @Autowired
    private IFetvNavigationContextService fetvNavigationContextService;

    /**
     * 获得id和name
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "query", value = "", required = false)
    })
    @ApiOperation(value = "查询栏目列表的标签", notes = "查询栏目列表的标签", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @ApiResponse(code = 200, message = "", response = List.class)
    @GetMapping("/label")
    public List<Label> list(@RequestParam(value = "query", required = false) String name) {
        if (name == null) {
            name = "";
        }
        return fetvNavigationContextService.getIdAndNameByName(name);
    }

    /**
     * 根据id获得name
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "String", name = "id", value = "", required = false)
    })
    @ApiResponse(code = 200, message = "", response = String.class)
    @ApiOperation(value = "查询栏目的名字", notes = "查询栏目的名字", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @GetMapping("/name/{id}")
    public String getNameById(@PathVariable(value = "id") Long id) {
        return fetvNavigationContextService.getNameById(id);
    }

    /**
     * 查询栏目列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "FetvNavigationContext", name = "fetvNavigationContext", value = "", required = true)
    })
    @ApiOperation(value = "查询栏目列表", notes = "查询栏目列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @ApiResponse(code = 200, message = "", response = TableDataInfo.class)
    @GetMapping("/list")
    public TableDataInfo<List<RespVO>> list(FetvNavigationContext fetvNavigationContext) {
        startPage();
        List<RespVO> list = fetvNavigationContextService.selectFetvNavigationContextList(fetvNavigationContext);
        return getDataTable(list);
    }


    /**
     * 获取栏目详细信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "获取栏目详细信息", notes = "获取栏目详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:query')")
    @GetMapping(value = "/{id}")
    @ApiResponse(code = 200, message = "", response = RespVO.class)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        RespVO data = fetvNavigationContextService.selectFetvNavigationContextById(id);
        if (data == null) {
            throw new ServiceException("不存在的ID");
        }
        return success(data);
    }

    /**
     * 新增栏目
     */

    @ApiOperation(value = "新增栏目信息", notes = "新增栏目信息", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('fetv:context:add')")
    @Log(title = "栏目", businessType = BusinessType.INSERT)
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    @PostMapping
    public AjaxResult add(@ApiParam(name = "fetvNavigationContext", value = "栏目信息", required = true) @RequestBody @Valid AddReq fetvNavigationContext) {
        return toAjax(fetvNavigationContextService.insertFetvNavigationContext(fetvNavigationContext));
    }

    /**
     * 修改栏目
     */


    @ApiOperation(value = "修改栏目信息", notes = "修改栏目信息", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('fetv:context:edit')")
    @Log(title = "栏目", businessType = BusinessType.UPDATE)
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    @PutMapping
    public AjaxResult edit(@ApiParam(name = "fetvNavigationContext", value = "栏目信息", required = true) @RequestBody @Valid EditReq fetvNavigationContext) {
        return toAjax(fetvNavigationContextService.updateFetvNavigationContext(fetvNavigationContext));
    }

    /**
     * 删除栏目
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Long[]", name = "ids", value = "", required = true)
    })
    @ApiOperation(value = "删除栏目信息", notes = "删除栏目信息", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('fetv:context:remove')")
    @Log(title = "栏目", businessType = BusinessType.DELETE)
    @ApiResponse(code = 200, message = "成功操作的数量", response = int.class)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fetvNavigationContextService.deleteFetvNavigationContextByIds(ids));
    }


    /**
     * 查询父或子栏目列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "FetvNavigationContext", name = "fetvNavigationContext", value = "", required = true)
    })
    @ApiOperation(value = "根据parentId查询子栏目列表", notes = "根据parentId查询子栏目列表,0与空为主栏目", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('fetv:context:list')")
    @ApiResponse(code = 200, message = "", response = TableDataInfo.class)
    @GetMapping("/list/primary")
    public TableDataInfo<List<RespVO>> listPrimary(FetvNavigationContext fetvNavigationContext) {
        startPage();
        List<RespVO> list = fetvNavigationContextService.selectFetvNavigationContextListLevel(fetvNavigationContext);
        return getDataTable(list);
    }
}
